﻿// Copyright (C) 2012 Jeff Tanner <jeff00seattle@gmail.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;

using WindowsAzure_REST;

namespace WindowsAzure_FileBackup_Common
{
    partial class Common
    {
        /// <summary>
        /// Allowed file system types
        /// </summary>
        public enum EnumServiceStatus
        {
            /// <summary>
            /// Service but Stopped
            /// </summary>
            ServiceInstalledStopped = 1,
            /// <summary>
            /// Service and Started
            /// </summary>
            ServiceInstalledStarted = 2,
            /// <summary>
            /// Service Not Installed
            /// </summary>
            ServiceNotInstalled = 3,
            /// <summary>
            /// Unknown Service Condition
            /// </summary>
            Unknown
        }

        public static bool DoesServiceExist(string serviceName) 
        {
            ServiceController service = null;
            string status = "";
            try {
                service = new ServiceController(serviceName);
                status = service.Status.ToString();
                return true; 
            } 
            catch (InvalidOperationException) 
            { 
                return false; 
            } 
            finally 
            {
                if (service != null) 
                {
                    service.Dispose(); 
                } 
            } 
        }

        public static string GetServiceStatus(string serviceName, out EnumServiceStatus serviceStatus)
        {
            ServiceController service = null;
            string status = "";
            serviceStatus = EnumServiceStatus.Unknown;
            try
            {
                service = new ServiceController(serviceName);
                if (service.Status.ToString().ToLower() == "stopped")
                {
                    status = "Installed but stopped";
                    serviceStatus = EnumServiceStatus.ServiceInstalledStopped;
                }

                if (service.Status.ToString().ToLower() == "running")
                {
                    status = "Installed and started";
                    serviceStatus = EnumServiceStatus.ServiceInstalledStarted;
                }
            }
            catch (InvalidOperationException)
            {
                status = "Service not installed";
                serviceStatus = EnumServiceStatus.ServiceNotInstalled;
            }
            finally
            {
                if (service != null)
                {
                    service.Dispose();
                }
            }

            return status;
        }

        public static void StartService(string serviceName, int timeoutMilliseconds)
        {
            ServiceController service = null;
            try
            {
                service = new ServiceController(serviceName);
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
            }
            catch
            {
                // ...
            }
            finally
            {
                if (service != null)
                {
                    service.Dispose();
                }
            } 
        }
        public static void StopService(string serviceName, int timeoutMilliseconds)
        {
            ServiceController service = null;
            try
            {
                service = new ServiceController(serviceName);
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
            }
            catch
            {
                // ...
            }
            finally
            {
                if (service != null)
                {
                    service.Dispose();
                }
            } 
        }

        public static void RestartService(string serviceName, int timeoutMilliseconds)
        {
            ServiceController service = null;
            try
            {
                service = new ServiceController(serviceName);
                int millisec1 = Environment.TickCount;
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

                // count the rest of the timeout
                int millisec2 = Environment.TickCount;
                timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));

                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
            }
            catch
            {
                // ...
            }
            finally
            {
                if (service != null)
                {
                    service.Dispose();
                }
            } 
        }
    }
}
